package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.ProcessConfigDTO;
import com.example.demo.dto.ProcessDetailDTO;
import com.example.demo.request.CreateProcessConfigRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.ProcessConfigService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/process-configs")

public class ProcessConfigController {

    @Autowired
    private ProcessConfigService processConfigService;


    @GetMapping
    public ResponseEntity<ApiResponse<List<ProcessConfigDTO>>> getProcessConfigs(
             @RequestParam(defaultValue = "0") int page,
             @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        List<ProcessConfigDTO> processConfigs = processConfigService.getProcessConfigs(page, size);
        long total = processConfigService.countProcessConfigs();
        // PageExtra的构造函数会自动计算totalPages
        PageExtra pageExtra = new PageExtra(page, size, total);
        
        return ResponseEntity.ok(
                ApiResponse.success(processConfigs, "查询成功", request.getRequestURI(), pageExtra)
        );
    }


    @GetMapping("/{id}/detail")
    public ResponseEntity<ApiResponse<ProcessDetailDTO>> getProcessDetail(
            @PathVariable Long id,
            HttpServletRequest request) {
        ProcessDetailDTO detailDTO = processConfigService.getProcessDetail(id);
        if (detailDTO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(
                ApiResponse.success(detailDTO, "查询成功", request.getRequestURI(), null)
        );
    }

    @PostMapping
    public ResponseEntity<ApiResponse<ProcessConfigDTO>> createProcessConfig(
            @RequestBody CreateProcessConfigRequest request,
            HttpServletRequest requestObj) {
        ProcessConfigDTO processConfigDTO = processConfigService.createProcessConfig(request);
        return ResponseEntity.status(201).body(
                ApiResponse.created(processConfigDTO, "流程配置创建成功", requestObj.getRequestURI())
        );
    }


    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ProcessConfigDTO>> updateProcessConfig(
             @PathVariable Long id,
            @RequestBody CreateProcessConfigRequest request,
            HttpServletRequest requestObj) {
        ProcessConfigDTO processConfigDTO = processConfigService.updateProcessConfig(id, request);
        if (processConfigDTO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(
                ApiResponse.success(processConfigDTO, "流程配置更新成功", requestObj.getRequestURI(), null)
        );
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteProcessConfig(
             @PathVariable Long id,
            HttpServletRequest requestObj) {
        processConfigService.deleteProcessConfig(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "流程配置删除成功", requestObj.getRequestURI(), null)
        );
    }
}